<?php
/**
 * @title 1143. 最长公共子序列 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $text1
     * @param String $text2
     * @return Integer
     */
    function longestCommonSubsequence($text1, $text2) {
        /**
         * @since 2024-01-03 字符串长度
         */
        $this->len1 = strlen($text1);
        $this->len2 = strlen($text2);
        $this->text1 = $text1;
        $this->text2 = $text2;

        /**
         * @since 2024-01-03 避免重复计算
         */
        $this->dp = [];

        /**
         * @since 2024-01-03 最长公共子序列
         * @return Integer
         */
        return $this->SearchString(0, 0);
    }

    /**
     * @title 返回最长公共子序列
     * @author start2004
     * @since 2024-01-03 Created
     *
     * @param Integer $p1
     * @param Integer $p2
     * @return Integer
     */
    function SearchString($p1, $p2){
        /**
         * @since 2024-01-03 是否命中缓存
         */
        $key = "{$p1}-{$p2}";
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2024-01-03 某个字符串遍历结束，递归终止条件
         */
        if($p1 == $this->len1 OR $p2 == $this->len2) {
            return 0;
        } else {}

        /**
         * @since 2024-01-03 如果指针位置字符相同，递归+1，递归
         */
        if($this->text1[$p1] == $this->text2[$p2]) {
            return $this->dp[$key] = $this->SearchString($p1 + 1, $p2 + 1) + 1;
        } else {}

        /**
         * @since 2024-01-03 在$text2找字符，去匹配$text1[$p1]
         */
        $maxLength1 = $maxLength2 = 0;
        for($i=$p2; $i<$this->len2; $i++){
            if($this->text2[$i] == $this->text1[$p1]){
                $maxLength1 = $this->SearchString($p1+1, $i+1) + 1;
                break;
            } else {}
        }

        /**
         * @since 2024-01-03 跳过$text1[$p1]，需要一个不同的字符，否则还不如匹配$text1[$p1]
         */
        for($i=$p1+1; $i<$this->len1; $i++){
            /**
             * @since 2024-01-03 找到不同字符
             */
            if($this->text1[$i] != $this->text1[$p1]){
                $maxLength2 = $this->SearchString($i, $p2);
                break;
            } else {}
        }

        /**
         * @return Integer
         */
        return $this->dp[$key] = max($maxLength1, $maxLength2);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.03_15.php
 */
$datas = [
    ["whydynkfslsrmvyhonyjenyrenojofafmnafmfyhyjebwhqpwh", "ctqdkfctanypmxqxktqfwfgnwjqpsbgpydovufgfqbyvqpufujy"],
    // ["abcde", "ace" ], // 3
    // ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->longestCommonSubsequence(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
